package com.mj.app.health.customer;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.mj.app.health.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 自定义首页按钮
 * Created by David Hogan on 2017/8/31.
 */

public class MainTabBtn extends FrameLayout {

    @BindView(R.id.main_tab_img)
    ImageView mainTabImg;
    @BindView(R.id.main_tab_txt)
    TextView mainTabTxt;

    private Context context;
    private Class<?> mClass;
    private Fragment mFragment;

    private float defImgWidth = getResources().getDimension(R.dimen.dm055);
    private float defImgHeight = getResources().getDimension(R.dimen.dm055);
    private float defTxtSize = getResources().getDimension(R.dimen.dm020);

    public MainTabBtn(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public MainTabBtn(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
        initXml(attrs);
    }

    public MainTabBtn(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
        initXml(attrs);
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.customer_main_tab, this, true);
        ButterKnife.bind(this, this);
    }

    private void initXml(AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MainTabBtn);
        if (typedArray.hasValue(R.styleable.MainTabBtn_img)) {
            mainTabImg.setImageResource(typedArray.getResourceId(R.styleable.MainTabBtn_img, 0));
        }

        if (typedArray.hasValue(R.styleable.MainTabBtn_txt)) {
            mainTabTxt.setText(typedArray.getText(R.styleable.MainTabBtn_txt));
        }else{
            mainTabTxt.setVisibility(GONE);
        }

        if(typedArray.hasValue(R.styleable.MainTabBtn_txtSizeBtn)){
            double b = typedArray.getDimension(R.styleable.MainTabBtn_txtSizeBtn, defTxtSize);
//            mainTabTxt.setTextSize(typedArray.getDimension(R.styleable.Main_Tab_Btn_txtSize, defTxtSize));
            mainTabTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.MainTabBtn_txtSizeBtn, defTxtSize));
        }

        if(typedArray.hasValue(R.styleable.MainTabBtn_txtColorBtn)){
//            mainTabTxt.setTextColor(typedArray.getColor(R.styleable.MainTabBtn_txtColorBtn, Color.parseColor("#222222")));
//            mainTabTxt.setTextColor(typedArray.getColor(R.styleable.MainTabBtn_txtColorBtn, Color.parseColor("#222222")));
            mainTabTxt.setTextColor(typedArray.getColor(R.styleable.MainTabBtn_txtColorBtn, Color.parseColor("#222222")));
        }

        if(typedArray.hasValue(R.styleable.MainTabBtn_imgWidth)){
            mainTabImg.getLayoutParams().width = (int)typedArray.getDimension(R.styleable.MainTabBtn_imgWidth, defImgWidth);
        }else{
            mainTabImg.getLayoutParams().width = (int) defImgWidth;
        }

        if(typedArray.hasValue(R.styleable.MainTabBtn_imgHeight)){
            mainTabImg.getLayoutParams().height = (int)typedArray.getDimension(R.styleable.MainTabBtn_imgHeight, defImgHeight);
        }else{
            mainTabImg.getLayoutParams().height = (int) defImgHeight;
        }

        typedArray.recycle();
    }

    public Class<?> getmClass() {
        return mClass;
    }

    public void setmClass(Class<?> mClass) {
        this.mClass = mClass;
    }

    public Fragment getmFragment() {
        return mFragment;
    }

    public void setmFragment(Fragment mFragment) {
        this.mFragment = mFragment;
    }
}
